<template>
  <!-- 操作会议 -->
  <base-breadcrumb>
    <a-spin :spinning="loading">
      <!-- 会议信息 -->
      <a-card>
        <title-name :title="$route.query.meetingName || form.meetingName" />
        <custom-form
          ref="form"
          :is-validate="true"
          :edit-model="''"
          :rules="rules"
          :form="form"
          :form-list="formOption"
        >
          <template slot="signNum">
            <div class="operation-conference-btn">
              <BaseInput
                v-model="form.signNum"
                disabled
              />
              <a-button
                type="primary"
                @click="signInVsible = true"
              > 查看签到 </a-button>
              <ConferenceScreen
                v-if="pageType !== 'view'"
                :id="id"
                :meeting-screen="training.meetingScreen"
              />
            </div>
          </template>
        </custom-form>
      </a-card>
      <!-- 会议信息 -->
      <!-- 相关资料上传 -->
      <a-card>
        <title-name title="相关资料上传" />
        <UploadTableNew
          ref="uploadTable"
          accept=".pdf"
          :disabled="pageType !== 'join'"
          :mode="file.mode"
          :init-file-list="file.fileList"
          :need-page="file.needPage"
          :business-code="file.businessCode"
        >
          <span
            v-if="pageType === 'join'"
            slot="head"
            class="ml12"
            style="color: #999"
          >请上传PDF格式文件！</span>
        </UploadTableNew>
      </a-card>
      <!-- 相关资料上传 -->
      <div class="tool-btns page-btn-right-top">
        <a-button
          v-if="pageType === 'detail'"
          class="ml20"
          :loading="submitLoading"
          type="primary"
          @click="handleStart"
        >
          开始会议
        </a-button>
        <a-button
          v-if="pageType === 'join'"
          class="ml20"
          :loading="submitLoading"
          type="primary"
          @click="handleSubmit"
        >
          提交
        </a-button>
        <BasePrintButton
          v-if="pageType === 'view'"
          :beforePrintFn="print"
          class="ml20"
        />
        <!-- <a-button
        v-if="pageType === 'view'"
        class="ml20"
        :loading="submitLoading"
        type="primary"
      >
        打印
      </a-button> -->
        <a-button
          class="ml20"
          @click="handleClose"
        > 关闭 </a-button>
      </div>
      <!-- 查看签到模态框 -->
      <SeeSign
        :id="id"
        :visible="signInVsible"
        :is-new="true"
        @cancel="signInVsible = false"
        @confirm="signInVsible = false"
      />
      <!-- 查看签到模态框 -->
    </a-spin>
  </base-breadcrumb>
</template>
<script>
import { trainType, meetingTypeNumber } from './model.js'
import SeeSign from '@/components/seeSign'
import training from '@/api/design/training'
import ConferenceScreen from '@/components/ConferenceScreen'
import BasePrintButton from '@/components/BasePrintButton' // 打印按钮
export default {
  name: 'a' + Date.now(),
  props: {
    isAudit: {
      type: Boolean,
      default: false
    }
  },
  components: {
    SeeSign,
    ConferenceScreen,
    BasePrintButton
  },
  data() {
    return {
      // 页面加载
      loading: true,
      // 表单
      form: {
        meetingEndTime: void 0,
        branchComName: '',
        projectName: '',
        meetingName: '',
        speakerId: '',
        meetingPlace: '',
        projectDesignType: '',
        meetingTime: null,
        createUser: '',
        createTime: null,
        signNum: '',
        meetingSummary: '',
        picUploadList: [],
        viewName: ""
      },
      // 提交加载
      submitLoading: false,
      // 相关资料
      file: {
        // 处理操作的模式：'add'模式下，删除操作会调用文件删除接口，'edit'模式只会在表格中移除删除数据，不会调用接口
        mode: 'add',
        // 上传文件的业务编码
        businessCode: 'DESIGN01',
        // 是否需要分页
        needPage: false,
        // 初始化文件列表
        fileList: []
      },
      // 签到id
      id: this.$route.query.id,
      // 签到模态框显示隐藏
      signInVsible: false,
      // 页面前置条件
      isReady: false,
      // 接口数据
      meetingById: {},
      // 接口
      training
    }
  },
  computed: {
    // 页面类型
    pageType() {
      return this.isAudit ? 'view' : this.$route.query.pageType
    },
    // 表单配置
    formOption() {
      const option1 = [
        {
          label: '所属单位',
          keyName: 'viewName',
          type: 'BaseInput',
          disabled: true,
          span: 16
        },
        // {
        //   label: '项目名称',
        //   keyName: 'projectName',
        //   type: 'BaseInput',
        //   disabled: true
        // },
        {
          label: '会议名称',
          keyName: 'meetingName',
          type: 'BaseInput',
          disabled: true
        },
        {
          label: '主持人',
          keyName: 'speaker',
          type: 'BaseInput',
          disabled: true
        },
        {
          label: '会议地点',
          keyName: 'meetingPlace',
          type: 'BaseInput',
          wrap: true,
          disabled: true
        }
      ]
      const option2 = [
        {
          label: '召开时间',
          keyName: 'meetingTime',
          type: 'BaseInput',
          disabled: true
        },
        {
          label: '记录人',
          keyName: 'createUser',
          type: 'BaseInput',
          disabled: true
        },
        {
          label: '记录时间',
          keyName: 'createTime',
          type: 'BaseInput',
          disabled: true
        },
        {
          label: '参会人员',
          keyName: 'signNum',
          slot: true
        },
        {
          label: '结束时间',
          keyName: 'meetingEndTime',
          type: 'a-date-picker',
          hidden: this.pageType === 'detail',
          disabled: this.pageType === 'view' || this.$route.query.meetingStatus === '40',
          attrs: {
            showTime: { format: 'HH:mm:ss' },
            format: 'YYYY-MM-DD HH:mm:ss',
            valueFormat: 'YYYY-MM-DD HH:mm:ss'
          },
          rules: [{ required: true, message: '请选择结束时间', trigger: 'change' }]
        },
        {
          label: '会议纪要',
          keyName: 'meetingSummary',
          type: 'BaseTextarea',
          autoSize: { minRows: 20, maxRows: 25 },
          attrs: {
            maxLength: 30000
          },
          rules: [{ required: true, message: '请输入', trigger: 'change' }],
          disabled: this.pageType !== 'join',
          span: 24
        },
        {
          keyName: 'picUploadList',
          hidden: this.pageType === 'detail',
          label: '图片上传',
          businessCode: 'DESIGN01',
          attrs: {
            uploadType: 'image',
            editModel: this.pageType === 'view' ? '0' : '1'
          },
          type: 'uploadFile',
          rules: [{ required: true, type: 'array', message: '请上传图片', trigger: 'change' }]
        }
      ]
      let list = []
      switch (this.isAudit ? 'trainingConference' : this.$route.query.pageName) {
        // 设计培训管理会
        case 'trainingConference':
          list = list.concat(
            option1,
            [
              {
                label: '培训分类',
                keyName: 'trainType',
                type: 'a-select',
                data: trainType,
                disabled: true
              }
            ],
            option2
          )
          break
        // 设计策划联合评审会
        case 'planningJoint':
          list = list.concat(
            option1,
            [
              {
                label: '项目设计管理类别',
                keyName: 'projectDesignType',
                type: 'BaseInput',
                disabled: true
              }
            ],
            option2
          )
          break
        // 设计策划启动会
        case 'planningStart':
          list = list.concat(
            option1,
            [
              {
                label: '项目设计管理类别',
                keyName: 'projectDesignType',
                type: 'BaseInput',
                disabled: true
              }
            ],
            option2
          )
          break
        // 设计管理复盘会
        case 'analyseConference':
          list = list.concat(option1, option2)
          break
      }
      return list
    },
    // 校验
    rules() {
      const obj = {}
      this.formOption.map((item) => {
        if (item.rules) {
          obj[item.keyName] = item.rules
        }
      })
      return obj
    }
  },
  mounted() {
    this.getConferenceDetail()
  },
  methods: {
    print() {
        return training.print({id:this.id}).then((res) => {
            return res.data.content
        })
    },
    /**
     * @description: 获取会议详情
     * @param {void}
     * @return {void}
     */
    async getConferenceDetail() {
      try {
        const { data } = await training.getMeetingById({ id: this.$route.query.id })
        this.meetingById = data
        this.file.fileList = data.uploadList
        this.form = Object.assign({}, this.form, data)
        this.form.viewName = this.form.branchComName + '/' + this.form.projectName
        this.loading = false
      } catch (error) {
        this.loading = false
        console.log(error)
      }
    },
    /**
     * @description: 关闭
     * @param {void}
     * @return {void}
     */
    handleClose() {
      this.$multiTab.close(this.$route.fullpath)
    },
    /**
     * @description: 会议开始
     * @param {void}
     * @return {void}
     */
    handleStart() {
      this.submitLoading = true
      training
        .startMeeting({ meetingId: this.$route.query.id })
        .then(({ code, message }) => {
          if (code === 200) {
            this.$message.success('开始成功')
            this.handleClose()
          } else {
            this.$message.warning(message)
          }
        })
        .finally(() => {
          this.submitLoading = false
        })
    },
    /**
     * @description: 提交
     * @param {void}
     * @return {void}
     */
    handleSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.file.fileList && this.file.fileList.length > 0) {
            this.form.picUploadList.length &&
              this.form.picUploadList.map((item) => {
                item.id && delete item.id
              })
            this.submitLoading = true
            let param = { id: this.$route.query.id,
                meetingType: this.form.meetingType,
                meetingSummary: this.form.meetingSummary,
                signNum: this.form.signNum,
                projectId: this.form.projectId,
                picUploadList: this.form.picUploadList,
                meetingEndTime: this.form.meetingEndTime,
                uploadList: this.file.fileList.map((el) => {
                  delete el.id
                  return el
                })}
                if (this.$route.query.meetingStatus === '40'){
                  training.updateSelective(param).then(({ code, message }) => {
                if (code === 200) {
                  this.$message.success('提交成功')
                  this.handleClose()
                } else {
                  this.$message.warning(message)
                }
              })
              .finally(() => {
                this.submitLoading = false
              })
                }else{
                  training.updateByPrimaryKeySelective(param).then(({ code, message }) => {
                if (code === 200) {
                  this.$message.success('提交成功')
                  this.handleClose()
                } else {
                  this.$message.warning(message)
                }
              })
              .finally(() => {
                this.submitLoading = false
              })
                }
            // training
            //   .updateByPrimaryKeySelective(param)
              // .then(({ code, message }) => {
              //   if (code === 200) {
              //     this.$message.success('提交成功')
              //     this.handleClose()
              //   } else {
              //     this.$message.warning(message)
              //   }
              // })
              // .finally(() => {
              //   this.submitLoading = false
              // })
          } else {
            this.$message.warning('请填写完所有必填项信息')
          }
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.operation-conference-btn {
  display: flex;
  align-items: center;
  .BaseInput-container {
    flex: 1;
  }
  .ant-btn {
    height: 28px;
    margin-left: 10px;
  }
}
</style>
